<?php

namespace app\admin\controller;


use think\Request;
use think\Db;
use think\cache\Driver\File;

class Article extends AdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list = Db::name('article')->paginate(3);
        // var_dump($list);
        // die;
        return view('article/index',[
            'list' => $list
        ]);
    }

    public function details($id)
    {

        $list = Db::name('article')->where('art_id',$id)->find();

        $data = Db::name('column')->where('column_id','=',$list['column_id'])->find();

        return view('article/details',[
            'list' => $list,
            'data'=>$data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $list = Db::name('column')->where('column_id','<>',1)->select();

        return view('article/add',[
            'list'=>$list
        ]);
    }

    //图片上传的ajax操作
    public function server()
    {
        $name = isset($_POST['name'])? $_POST['name'] : '';
        $gender = isset($_POST['gender'])? $_POST['gender'] : '';

        $filename = md5(rand()).time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));

        $response = array();

        if(move_uploaded_file($_FILES['photo']['tmp_name'], './static/upload/'.$filename)){
            $response['isSuccess'] = true;
            $response['name'] = $name;
            $response['gender'] = $gender;
            $response['photo'] = $filename;

        }else{
            $response['isSuccess'] = false;
        }

        echo json_encode($response);
    }

   

    public function save(Request $request)
    {

       $p = $request->post();

       // var_dump($p);
       // die();
       $data = [
            'artname' => $p['artname'],
            'arttime' => date('Y-m-d h:m:s'),
            'column_id' => $p['column_id'],
            'artcontent' => $p['artcontent'],
            'arttext' => $p['arttext'],
            'artpic' => $p['picname'],


        ];
        $result = Db::name('article')->data($data)->insert();
        // if ($result) {
        //     return $this->success('添加成功(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 飞天了！', url('admin/article/index'));
        // }else{
        //     return $this->error('添加失败(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 坠机了！', url('admin/article/add'));

        // }
        return $this->redirect('admin/article/index');

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {

        $data = Db::name('article')->find($id);
        // var_dump($data);
        // die;
        return view('article/edit',[
            'data'=> $data
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {

        $p = $request->post();
        //判断是否上传了新的图片
        if(!empty($p['artpic'])){
            //上传新图片删除旧图片
            $data = Db::name('article')->find($id);
            if(!empty($data['artpic'])){
                $url = './static/upload/'.$data['artpic'];
                // File->unlink($url);
                unlink($url);
            }
            $data = [
                'artname' => $p['artname'],
                'column_id' => $p['column_id'],
                'artcontent' => $p['artcontent'],
                'arttext' => $p['arttext'],
                'artpic' => $p['artpic']

            ];

        }else{

            $data = [
                'artname' => $p['artname'],
                'column_id' => $p['column_id'],
                'artcontent' => $p['artcontent'],
                'arttext' => $p['arttext'],
            ];
        }
        $result = Db::name('article')->where('art_id',$id)->update($data);
        // if ($result) {
        //     return $this->success('修改文章成功(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 飞天了！', url('admin/article/index'));
        // }else{
        //     return $this->error('修改文章失败(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 坠机了！', url('admin/article/index'));
        // }
            // return $this->success('保存文章成功(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 飞天了！', url('admin/article/index'));
        return $this->redirect('admin/article/index');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {

        $data = Db::name('article')->find($_POST['artid']);//获取当前文章的信息
        // var_dump($data);
        //根据文章id查出评论
        $acom = Db::name('artcomment')->where('art_id',$_POST['artid'])->select();
        // var_dump($acom);
         //判断文章是否有评论
        if(!empty($acom)){
            //有的话进行删除
            foreach($acom as $val){
                
                //根据评论的id查询追评数据
                $ardata =  Db::name('runcomment')->where('artcom_id',$val['artcom_id'])->select();
                // var_dump($ardata);
                // die;
                //判断追评是否为空
                if(!empty($ardata)){
                    //为空的话遍历删除追评
                    // var_dump($ardata);
                    // die;
                    foreach($ardata as $v){
                        // var_dump($v);
                        // die;
                        $result = Db::name('runcomment')->where('runcom_id',$v['runcom_id'])->delete(); 
                    }

                }

                $result = Db::name('artcomment')->where('artcom_id',$val['artcom_id'])->delete();//删除评论
  
            }
            
        }
        
        if(!empty($data['artpic'])){
            $url = './static/upload/'.$data['artpic'];
            unlink($url);
        }

        $result = Db::name('article')->delete($_POST['artid']);

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        $info['status'] = true;
        return json($info);
        
    }
}
